package com.gmlive.common.sample.widgets

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.lifecycle.lifecycleScope
import com.gmlive.common.sample.R
import com.gmlive.common.sample.views.BulgeTabLayout
import com.gmlive.common.sample.views.CircularImageView
import com.gmlive.common.sample.views.ipx
import com.google.android.material.badge.BadgeDrawable
import kotlinx.android.synthetic.main.activity_bulge_tab.*
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class BulgeTabActivity : AppCompatActivity() {
    private val titles = arrayOf("首页", "消息", "我的", "工作")
    private val pageTags = arrayOf("home", "message", "mine", "work")
    private val drawables = arrayOf(
        R.drawable.tab_home,
        R.drawable.tab_message,
        R.drawable.tab_mine,
        R.drawable.tab_mine
    )

    private val tabConfiguration: BulgeTabLayout.TabConfigurationStrategy =
        object : BulgeTabLayout.TabConfigurationStrategy {
            override fun onConfigureTab(tab: BulgeTabLayout.Tab, position: Int) {
                // tab.position = position
                tab.text = titles[position]
                tab.setIcon(drawables[position])
                tab.getOrCreateBadge().apply {
                    maxCharacterCount = 3
                    number = if (position == 2) 199 else position
                    if (position == 0) {
                        clearNumber()
                    }
                    badgeGravity = BadgeDrawable.TOP_END

                    // 正值会向anchor中心偏移
                    // horizontalOffset = 1.ipx
                    // verticalOffset = 6.ipx
                }
            }
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_bulge_tab)

        viewPager.adapter = TabPagerAdapter(supportFragmentManager)

        tabLayout.setupWithViewPager(viewPager, tabConfiguration)
    }

    class TabPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {
        private val titles = arrayOf("首页", "消息", "我的", "工作")

        override fun getCount(): Int = titles.size

        override fun getPageTitle(position: Int): CharSequence = titles[position]

        override fun getItem(position: Int): Fragment {
            return TabFragment.newInstance(getPageTitle(position))
        }
    }

    class TabFragment : Fragment(R.layout.fragment_tab_item) {
        companion object {
            fun newInstance(text: CharSequence): TabFragment {
                return TabFragment().apply {
                    arguments = bundleOf("extra_text" to text)
                }
            }
        }

        private var text: CharSequence? = "null"

        override fun onAttach(context: Context) {
            super.onAttach(context)
            text = arguments?.getCharSequence("extra_text") ?: "nil"
        }

        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
            view.findViewById<TextView>(R.id.text).text = text

//            val imageView = view.findViewById<CircularImageView>(R.id.imageView)
//            lifecycleScope.launch {
//                delay(500)
//                imageView.setImageResource(R.drawable.button_start_match)
//            }
        }
    }
}